<template>
  <div class="register-container">
    <h2>用户注册</h2>
    <form @submit.prevent="handleRegister">
      <div>
        <label>用户名：</label>
        <input v-model="username" required />
      </div>
      <div>
        <label>邮箱：</label>
        <input v-model="email" type="email" required />
      </div>
      <div>
        <label>密码：</label>
        <input type="password" v-model="password" required />
      </div>
      <button type="submit">注册</button>
  <div v-if="error" class="error" style="color:#ff5000;font-weight:bold;margin-top:8px;white-space:pre-line;">{{ error }}</div>
      <div v-if="success" class="success">{{ success }}</div>
    </form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import axios from '../api/axios';
import { useRouter } from 'vue-router';

const username = ref('');
const email = ref('');
const password = ref('');
const error = ref('');
const success = ref('');
const router = useRouter();

const handleRegister = async () => {
  error.value = '';
  success.value = '';
  const res = await axios.post('/auth/register', {
      username: username.value,
      email: email.value,
      password: password.value,
    });
  try {
    console.log(res.data[0]);
    
    success.value = '注册成功，请登录';
    setTimeout(() => router.push('/login'), 1500);
  } catch (e: any) {
    // 优化：显示后端返回的 message 字段
    error.value = e.response?.data?.message || e.response?.data || '注册失败';
  }
};
</script>

<style scoped>
  .register-container {
    max-width: 400px;
    margin: 48px auto;
    padding: 32px 32px 24px 32px;
    border-radius: 16px;
    box-shadow: 0 4px 24px #ffe7ba;
    background: #fffbe6;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  h2 {
    text-align: center;
    color: #223344;
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 32px;
  }
  form {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  form > div {
    display: flex;
    align-items: center;
    margin-bottom: 18px;
    width: 100%;
  }
  label {
    width: 90px;
    text-align: right;
    font-size: 1.1rem;
    color: #223344;
    margin-right: 12px;
  }
  input {
    flex: 1;
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid #ffd591;
    font-size: 1rem;
    background: #fff;
    transition: border 0.2s;
  }
  input:focus {
    border-color: #ff6600;
    outline: none;
  }
  button[type="submit"] {
    margin-top: 12px;
    background: #ff6600;
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 10px 32px;
    font-size: 1.1rem;
    font-weight: bold;
    box-shadow: 0 2px 8px #ffd591;
    cursor: pointer;
    transition: background 0.2s;
  }
  button[type="submit"]:hover {
    background: #ff8500;
  }
  .error {
    color: #ff3c00;
    margin-top: 10px;
    font-size: 1rem;
    text-align: center;
  }
  .success {
    color: #009900;
    margin-top: 10px;
    font-size: 1rem;
    text-align: center;
  }
</style>
